package cn.itcast.service.impl;

import cn.itcast.dao.OrderDao;
import cn.itcast.dao.PaylogDao;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.PayLog;
import cn.itcast.service.PayService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private PaylogDao paylogDao;
    @Override
    public void updatePayStatus(Map<String, String> result) {
        String orderId = result.get("out_trade_no");
        String transactionId = result.get("transaction_id");
        Order order = orderDao.findById(orderId);
        order.setPayStatus(1);
        orderDao.update(order);

        //修改支付日志
        PayLog payLog = paylogDao.findByOrderId(orderId);
        if (payLog == null) {
            payLog = new PayLog();
            payLog.setOrderId(orderId);
            payLog.setCreateTime(new Date());
            payLog.setMoney(Double.parseDouble(result.get("total_fee")));
        }
        payLog.setPayStatus(1);
        payLog.setPayTime(new Date());
        payLog.setTransactionId(transactionId);
        paylogDao.updateByOrderId(payLog);
    }
}
